//
//  OrderViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/17.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "OrderViewController.h"
#import "OrderTableViewCell.h"
#import "TimeSelector.h"

@interface OrderViewController ()<UITableViewDelegate,UITableViewDataSource,SelectDelegate>
{
    NSString * type;
    NSString * property;
    NSString * orderbase;
    NSString * orderrule;
    NSString * page;
    NSString * timesss;
    NSString * nexttime;
    NSString * showtype;
    NSString * uid;
    
    UITableView * contentTableView;
    TimeSelector * times;
}
@end

@implementation OrderViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        timesss = @"";
    }
    return self;
}

-(void)backAction
{
    if (times)
    {
        [times removeFromSuperview];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    type = self.type;
    property = self.property;
    orderbase = self.orderbase;
    orderrule = self.orderrule;
    page = self.page;
    timesss = self.time;
    nexttime = self.nexttime;
    showtype = self.showtype;
    uid = self.uid;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(okAction)];
    
    self.navigationItem.title = @"筛选";
    
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height, ScreenWidth, ScreenHeight - rectStatus.size.height - rectNav.size.height) style:UITableViewStylePlain];
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:contentTableView];
    

}

-(void)okAction
{
    [self.delegate orderResultWithType:type property:property orderbase:orderbase orderrult:orderrule page:page time:timesss nexttime:nexttime showtype:showtype uid:uid];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - tableview

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    header.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    UILabel * headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth, 20)];
    headerLabel.font = [UIFont systemFontOfSize:15];
    if (section == 0)
    {
        headerLabel.text = @"时间选择";
    }
    else if (section == 1)
    {
        headerLabel.text = @"排序";
    }
    else if (section == 2)
    {
        headerLabel.text = @"类别";
    }
    else if (section == 3)
    {
        headerLabel.text = @"记录属性";
    }
    else if (section == 4)
    {
        headerLabel.text = @"可见性";
    }
    else
    {
        headerLabel.text = @"可见性";
    }
    [header addSubview:headerLabel];
    return header;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else if (section == 1)
    {
        return 2;
    }
    else if (section == 2)
    {
        return 7;
    }
    else if (section == 3)
    {
        return 5;
    }
    else
    {
        return 4;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [OrderTableViewCell height];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        static NSString * identifer = @"identifer";
        OrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[OrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        [cell loadData:@"时间选择" seleciton:NO textd:timesss];
        return cell;
    }
    else if (indexPath.section == 1)
    {
        static NSString * identifer = @"identifer";
        OrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[OrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        if (indexPath.row == 0)
        {
            if ([orderbase isEqualToString:@"0"])
            {
                [cell loadData:@"发布时间" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"发布时间" seleciton:NO textd:@""];
            }
        }
        else
        {
            if ([orderbase isEqualToString:@"1"])
            {
                [cell loadData:@"事件时间" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"事件时间" seleciton:NO textd:@""];
            }
        }

        return cell;
    }
    else if (indexPath.section == 2)
    {
        static NSString * identifer = @"identifer";
        OrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[OrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        if (indexPath.row == 0)
        {
            if ([type isEqualToString:@"6"])
            {
                [cell loadData:@"不限" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"不限" seleciton:NO textd:@""];
            }
        }
        else if (indexPath.row == 1)
        {
            if ([type isEqualToString:@"0"])
            {
                [cell loadData:@"个人大事记" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"个人大事记" seleciton:NO textd:@""];
            }
        }
        else if (indexPath.row == 2)
        {
            if ([type isEqualToString:@"1"])
            {
                [cell loadData:@"个人采风" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"个人采风" seleciton:NO textd:@""];
            }
        }
        else if (indexPath.row == 3)
        {
            if ([type isEqualToString:@"2"])
            {
                [cell loadData:@"校园活动" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"校园活动" seleciton:NO textd:@""];
            }
        }
        else if (indexPath.row == 4)
        {
            if ([type isEqualToString:@"3"])
            {
                [cell loadData:@"社会活动" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"社会活动" seleciton:NO textd:@""];
            }
        }
        else if (indexPath.row == 5)
        {
            if ([type isEqualToString:@"4"])
            {
                [cell loadData:@"技能资质" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"技能资质" seleciton:NO textd:@""];
            }
        }
        else
        {
            if ([type isEqualToString:@"5"])
            {
                [cell loadData:@"梦想舞台" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"梦想舞台" seleciton:NO textd:@""];
            }
        }
       
        return cell;
    }
    else if (indexPath.section == 3)
    {
        static NSString * identifer = @"identifer";
        OrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[OrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        if (indexPath.row == 0)
        {
            if ([property isEqualToString:@"4"])
            {
                [cell loadData:@"不限" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"不限" seleciton:NO textd:@""];
            }
        }
        else if (indexPath.row == 1)
        {
            if ([property isEqualToString:@"0"])
            {
                [cell loadData:@"个人" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"个人" seleciton:NO textd:@""];
            }
        }
        else if (indexPath.row == 2)
        {
            if ([property isEqualToString:@"2"])
            {
                [cell loadData:@"家庭" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"家庭" seleciton:NO textd:@""];
            }
        }
        else if (indexPath.row == 3)
        {
            if ([property isEqualToString:@"1"])
            {
                [cell loadData:@"伙伴" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"伙伴" seleciton:NO textd:@""];
            }
        }
        else
        {
            if ([property isEqualToString:@"3"])
            {
                [cell loadData:@"事业" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"事业" seleciton:NO textd:@""];
            }
        }
        return cell;
    }
    else
    {
        static NSString * identifer = @"identifer";
        OrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[OrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        if (indexPath.row == 0)
        {
            if ([showtype isEqualToString:@"0"])
            {
                [cell loadData:@"不限" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"不限" seleciton:NO textd:@""];
            }
        }
        else if (indexPath.row == 1)
        {
            if ([showtype isEqualToString:@"3"])
            {
                [cell loadData:@"全部可见" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"全部可见" seleciton:NO textd:@""];
            }
        }
        else if (indexPath.row == 2)
        {
            if ([showtype isEqualToString:@"2"])
            {
                [cell loadData:@"仅好友可见" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"仅好友可见" seleciton:NO textd:@""];
            }
        }
        else
        {
            if ([showtype isEqualToString:@"1"])
            {
                [cell loadData:@"仅自己可见" seleciton:YES textd:@""];
            }
            else
            {
                [cell loadData:@"仅自己可见" seleciton:NO textd:@""];
            }
        }
        return cell;
    }
}

-(void)selectedTime:(NSString *)time
{
    timesss = time;
    [contentTableView reloadData];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0)
    {
        CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
        CGRect rectNav = self.navigationController.navigationBar.frame;
        
        times = [[TimeSelector alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height, ScreenWidth, ScreenHeight - rectStatus.size.height - rectNav.size.height)];
        [self.view addSubview:times];
        times.delegate = self;
        [times requestData];
    }
    else if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            orderbase = @"0";
        }
        else
        {
            orderbase = @"1";
        }
    }
    else if (indexPath.section == 2)
    {
        if (indexPath.row == 0)
        {
            type = @"6";
        }
        else if (indexPath.row == 1)
        {
            type = @"0";
        }
        else if (indexPath.row == 2)
        {
            type = @"1";
        }
        else if (indexPath.row == 3)
        {
            type = @"2";
        }
        else if (indexPath.row == 4)
        {
            type = @"3";
        }
        else if (indexPath.row == 5)
        {
            type = @"4";
        }
        else
        {
            type = @"5";
        }
    }
    else if (indexPath.section == 3)
    {
        if (indexPath.row == 0)
        {
            property = @"4";
        }
        else if (indexPath.row == 1)
        {
            property = @"0";
        }
        else if (indexPath.row == 2)
        {
            property = @"2";
        }
        else if (indexPath.row == 3)
        {
            property = @"1";
        }
        else if (indexPath.row == 4)
        {
            property = @"3";
        }
    }
    else
    {
        if (indexPath.row == 0)
        {
            showtype = @"3";
        }
        else if (indexPath.row == 1)
        {
            showtype = @"0";
        }
        else if (indexPath.row == 2)
        {
            showtype = @"2";
        }
        else
        {
            showtype = @"1";
        }
    }
    [contentTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
